package cn.nubia.admin.common.ftp;

import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 有问题找翔
 * Created by wangj on 2014/4/30.
 */
public class FtpClientUtil {
    private static final Logger logger = LoggerFactory.getLogger(FtpClientUtil.class);

    private ObjectPool<FTPClientWrapper> pool;

    public FtpClientUtil(String ftpServer, String username, String password) {
        this.pool = new GenericObjectPool<FTPClientWrapper>(new FtpClientFactory(ftpServer , username , password ));
    }

    public FtpClientUtil(String ftpServer, String username, String password ,Boolean localActive ,Boolean binaryTransfer) {
        this.pool = new GenericObjectPool<FTPClientWrapper>(new FtpClientFactory(ftpServer , username , password , localActive ,binaryTransfer));
    }



    private interface InnerExec{
         <T> T exec(FTPClientWrapper ftpClientWrapper);
    }

    public <T> T normalExec (InnerExec innerExec) {
        long start = System.currentTimeMillis();
        FTPClientWrapper ftpClientWrapper = null;
        try {
            ftpClientWrapper = pool.borrowObject();
            return innerExec.exec(ftpClientWrapper);
        }catch (Exception e){
            logger.error("exec ftp task error:" , e);
        }finally {
            try {
                pool.returnObject(ftpClientWrapper);
            } catch (Exception e) {
                e.printStackTrace();
            }
            logger.info("ftp 耗时:"+(System.currentTimeMillis() - start)+"ms");
        }
        return null;
    }

    /**
     * 判断目录是否存在
     * @param dir
     * @return
     */
    public Boolean isDirExist(final String dir) {
        return normalExec(new InnerExec() {
            @Override
            public Boolean exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.isDirExist(dir);
            }
        });
    }

    public List<FtpFileItem> getFiles(final String dir) {
        List<FtpFileItem> ftpFileItems = normalExec(new InnerExec() {
            @Override
            public List<FtpFileItem> exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.getFileItems(dir);
            }
        });
        if (ftpFileItems == null) {
            ftpFileItems = new ArrayList<FtpFileItem>();
        }
        return ftpFileItems;
    }

    /**
     * 用流的注意了..使用的时候自己记得关掉
     *
     * @param dir      工作目录
     * @param filename 文件名
     * @param is       流
     */
    public Boolean addFile(final String dir,final String filename,final InputStream is) {
        return normalExec(new InnerExec() {
            @Override
            public Boolean exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.addFile(dir, filename ,is);
            }
        });
    }

    public Boolean addFile( final String dir,final String filename,final File file) {
        return normalExec(new InnerExec() {
            @Override
            public Boolean exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.addFile(dir ,filename ,file);
            }
        });
    }

    /**
     * 删除文件
     *
     * @param dir
     * @param filename
     */
    public Boolean deleteFile(final String dir,final String filename) {
        return normalExec(new InnerExec() {
            @Override
            public Boolean exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.deleteFile(dir, filename);
            }
        });
    }

    /**
     * 重命名文件
     * @param oldPath
     * @param newPath
     * @return
     */
    public Boolean renameFile(final String oldPath,final String newPath) {
        return normalExec(new InnerExec() {
            @Override
            public Boolean exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.renameFile(oldPath, newPath);
            }
        });
    }



    public File getFile(final String dir,final String filename,final File file) {
        return normalExec(new InnerExec() {
            @Override
            public File exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.getFile(dir, filename, file);
            }
        });
    }

    /**
     * 获取文件路径内容
     *
     * @param dir
     * @param filename
     * @return
     */
    public String getFileContent(final String dir,final String filename ,final String encode) {
        return normalExec(new InnerExec() {
            @Override
            public String exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.getFileContent(dir, filename, encode);
            }
        });
    }

    /**
     * 移除目录
     *
     * @param path
     */
    public Boolean deleteDir(final String path) {
        return normalExec(new InnerExec() {
            @Override
            public Boolean exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.deleteDir(path);
            }
        });


    }

    public Boolean createDir(final String dir,final String dirName) {
        return normalExec(new InnerExec() {
            @Override
            public Boolean exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.createDir(dir, dirName);
            }
        });
    }

    public Boolean createDirs(final String dir) {
        return normalExec(new InnerExec() {
            @Override
            public Boolean exec(FTPClientWrapper ftpClientWrapper) {
                return ftpClientWrapper.createDirs(dir);
            }
        });
    }

    public static void main(String[] args) {
        FtpClientUtil ftpClientUtil = new FtpClientUtil("10.5.117.111" , "resin" , "111111");
        boolean suc = ftpClientUtil.createDir("/" , "abcdefg");
        System.out.println("suc:"+suc);

    }
}
